<template>
  <div class="page-container">
    <div class="search mg_t2">

      <el-form :label-width="$globalCss.formLabelWidth_default" label-position="right" ref="formSearch" :rules="rules"
        :model="{}" onsubmit="return false">
        <el-form-item :label="$tt('label_package_weight')">
          <auto_weight_input v-model="autoWeight" style="width: 400px;"></auto_weight_input>
        </el-form-item>
        <el-form-item :label="$tt('label_package_code')" prop="searchCode">
          <div class="flex_row_center">
            <scan_input v-model.trim="searchCode" @enterInput="scanPackageNo" style="width:400px"
              class="form_input_default" autofocus>
            </scan_input>
            <el-button class="mg_l2" type="primary" @click="scanPackageNo">{{ $tt('label_scan') }}</el-button>
          </div>

        </el-form-item>

        <el-form-item :label="$tt('label_customer_name')">
          <el-input v-model="customerName" class="form_input_default" disabled />
        </el-form-item>
        <el-form-item :label="$tt('label_pms_supply')">
          <el-input v-model="supplyName" class="form_input_default" disabled />
        </el-form-item>
      </el-form>
    </div>

    <div class="flex_row flex_justify_sb mg_v2">
      <div class="flex_row">
      </div>
      <div class="flex_row">
        <!-- <el-button @click="batchSignOut" type="primary">{{ $tt('label_handover_shipping') }}</el-button>
        <el-button @click="goToRecord">{{ $tt('交接发运记录') }}</el-button> -->
      </div>
    </div>


    <ex_super_table ref="MainTable" class="main-table" v-bind="tableOption" :data="tableData" :autoFlex="true"
      :autoPage="false" @selection-change="list => selection = list">
    </ex_super_table>
  </div>
</template>
<script>
import auto_weight_input from '@/component/serial_port/auto_weight_input.vue'
export default {
  title: "label_pack_measure_weight",
  mixins: [],
  components: { auto_weight_input },
  data() {
    return {
      selection: [],
      searchCode: "",
      autoWeight: '',
      customerName: "",
      supplyName: "",

      rules: {
        autoWeight: {
          required: true, trigger: 'blur', validator: (r, v, cb) => {
            if (!this.autoWeight) {
              cb(this.$tt('tip_can_no_empty'))
            } else if (Number.isNaN(Number(this.autoWeight))) {
              cb(this.$t('tip_format_error'))
            } else {
              cb()
            }
          },
        },
        searchCode: {
          required: true, trigger: 'blur',
          validator: (r, v, cb) => {
            if (_.isEmpty(this.searchCode)) {
              return cb(this.$tt('tip_can_no_empty'))
            }
            cb()
          }
        },
      },



      tableData: [],
      tableOption: {

        model: [
          { key: 'packingNo', label: 'label_package_code', 'min-width': 140, },

          {
            key: 'weight', label: 'label_weight', 'width': 120, 
          },
          { key: 'ecPlatformName', label: 'label_ec_platform', 'width': 120, },

          { key: 'customerName', label: 'label_customer_info', 'width': 120, 'show-overflow-tooltip': true, },
          {
            key: 'supplierName', label: 'label_pms_supply', 'width': 100, 'show-overflow-tooltip': true
          },
          {
            key: 'orderNo', label: 'label_order_code', 'min-width': 140, 'show-overflow-tooltip': true
          },
          {
            key: 'pieceCount', label: 'label_tms_track_pkgs', 'width': 100, 'show-overflow-tooltip': true
          },
          {
            key: 'volume', label: 'label_volume', 'width': 100, 'show-overflow-tooltip': true
          },
          
          { key: 'addTime', label: 'label_operate_time', 'width': 150,formatter: (r, c, v, i) => this.$dateFormatTime(v) },
        ],

      }

    };
  },

  mounted() {

  },
  methods: {




    scanPackageNo() {
      this.$refs.formSearch.validate((valid) => {
        if (valid) {
          // if (this.tableData.some(it => it.packageNo == this.searchCode )) {
          //   return this.$message.error(this.$tt('tip_code_has_add', { code: this.searchCode }))
          // }
          this.getPackageNoInfo(Number(this.autoWeight))
        } else {
          return false;
        }
      });

    },
    getPackageNoInfo(weight) {
      this.$ajax({
        url: "/wms-api/outbound/OutboundPickingWeighRecordRpc/selectPackageInfoByPackageNo.json",
        method: 'post',
        data: {
          packageNo: this.searchCode
        },
        success: (res) => {
          if (res && res.data && res.data.packingNo) {
            this.customerName = res.data.customerName
            this.supplyName = res.data.supplierName
            this.savePackageNoInfo(res.data, weight)
          } else {
            this.$message.error(this.$tt('tip_not_found_data'))
          }
        },
        failure: () => {
        },
        complete: () => {
        }
      })
    },
    savePackageNoInfo(data, weight) {
      const params = _.cloneDeep(data)
      params.weight = weight
      this.$ajax({
        url: "/wms-api/outbound/OutboundPickingWeighRecordRpc/savePickingWeighRecord.json",
        method: 'post', type: "json",
        data: params,
        success: (res) => {
          this.$emitPage('outbound>pack_weigh_record', 'reload')

          this.$message.success(this.$tt('tip_submit_success'))
          let now = new Date().getTime()
          params.addTime = now
          const i = _.findIndex(this.tableData, it => it.packingNo == params.packingNo)
          if (i >= 0) {
            this.tableData.splice(i, 1,)
          }
          this.tableData.splice(0, 0, params)

        },
        failure: () => {
        },
        complete: () => {
        }
      })
    },
    goToRecord() {
      this.$router.push({ path: "/page/wms/overseas/outbound/handover_shipping_record", query: {} });
    },
  }

};

</script>
<style></style>
